home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / jpsrc2.zip / JCCOLOR.C < prev    next >
C/C++ Source or Header  |  1991-12-01  |  6KB  |  207 lines

  1. /*
  2.  * jccolor.c
  3.  *
  4.  * Copyright (C) 1991, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains input colorspace conversion routines.
  9.  * These routines are invoked via the methods get_sample_rows
  10.  * and colorin_init/term.
  11.  */
  12.  
  13. #include "jinclude.h"
  14.  
  15.  
  16. static JSAMPARRAY pixel_row;    /* Workspace for a pixel row in input format */
  17.  
  18.  
  19. /*
  20.  * Initialize for colorspace conversion.
  21.  */
  22.  
  23. METHODDEF void
  24. colorin_init (compress_info_ptr cinfo)
  25. {
  26.   /* Allocate a workspace for the result of get_input_row. */
  27.   pixel_row = (*cinfo->emethods->alloc_small_sarray)
  28.         (cinfo->image_width, (long) cinfo->input_components);
  29. }
  30.  
  31.  
  32. /*
  33.  * Fetch some rows of pixels from get_input_row and convert to the
  34.  * JPEG colorspace.
  35.  * This version handles RGB -> YCbCr conversion.
  36.  * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
  37.  * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
  38.  */
  39.  
  40. METHODDEF void
  41. get_rgb_ycc_rows (compress_info_ptr cinfo,
  42.           int rows_to_read, JSAMPIMAGE image_data)
  43. {
  44.   register INT32 r, g, b;
  45.   register JSAMPROW inptr0, inptr1, inptr2;
  46.   register JSAMPROW outptr0, outptr1, outptr2;
  47.   register long col;
  48.   long width = cinfo->image_width;
  49.   int row;
  50.  
  51.   for (row = 0; row < rows_to_read; row++) {
  52.     /* Read one row from the source file */
  53.     (*cinfo->methods->get_input_row) (cinfo, pixel_row);
  54.     /* Convert colorspace */
  55.     inptr0 = pixel_row[0];
  56.     inptr1 = pixel_row[1];
  57.     inptr2 = pixel_row[2];
  58.     outptr0 = image_data[0][row];
  59.     outptr1 = image_data[1][row];
  60.     outptr2 = image_data[2][row];
  61.     for (col = width; col > 0; col--) {
  62.       r = GETJSAMPLE(*inptr0++);
  63.       g = GETJSAMPLE(*inptr1++);
  64.       b = GETJSAMPLE(*inptr2++);
  65.       /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
  66.        * must be too; do not need an explicit range-limiting operation.
  67.        */
  68.       /* Y */
  69.       *outptr0++ = (JSAMPLE)
  70.     ((   306*r +  601*g +  117*b + (INT32) 512) >> 10);
  71.       /* Cb */
  72.       *outptr1++ = (JSAMPLE)
  73.     (((-173)*r -  339*g +  512*b + (INT32) 512*(MAXJSAMPLE+1)) >> 10);
  74.       /* Cr */
  75.       *outptr2++ = (JSAMPLE)
  76.     ((   512*r -  429*g -   83*b + (INT32) 512*(MAXJSAMPLE+1)) >> 10);
  77.     }
  78.   }
  79. }
  80.  
  81.  
  82. /*
  83.  * Fetch some rows of pixels from get_input_row and convert to the
  84.  * JPEG colorspace.
  85.  * This version handles grayscale (no conversion).
  86.  */
  87.  
  88. METHODDEF void
  89. get_grayscale_rows (compress_info_ptr cinfo,
  90.             int rows_to_read, JSAMPIMAGE image_data)
  91. {
  92.   int row;
  93.  
  94.   for (row = 0; row < rows_to_read; row++) {
  95.     /* Read one row from the source file */
  96.     (*cinfo->methods->get_input_row) (cinfo, pixel_row);
  97.     /* Convert colorspace (gamma mapping needed here) */
  98.     jcopy_sample_rows(pixel_row, 0, image_data[0], row,
  99.               1, cinfo->image_width);
  100.   }
  101. }
  102.  
  103.  
  104. /*
  105.  * Fetch some rows of pixels from get_input_row and convert to the
  106.  * JPEG colorspace.
  107.  * This version handles multi-component colorspaces without conversion.
  108.  */
  109.  
  110. METHODDEF void
  111. get_noconvert_rows (compress_info_ptr cinfo,
  112.             int rows_to_read, JSAMPIMAGE image_data)
  113. {
  114.   int row, ci;
  115.  
  116.   for (row = 0; row < rows_to_read; row++) {
  117.     /* Read one row from the source file */
  118.     (*cinfo->methods->get_input_row) (cinfo, pixel_row);
  119.     /* Convert colorspace (gamma mapping needed here) */
  120.     for (ci = 0; ci < cinfo->input_components; ci++) {
  121.       jcopy_sample_rows(pixel_row, ci, image_data[ci], row,
  122.             1, cinfo->image_width);
  123.     }
  124.   }
  125. }
  126.  
  127.  
  128. /*
  129.  * Finish up at the end of the file.
  130.  */
  131.  
  132. METHODDEF void
  133. colorin_term (compress_info_ptr cinfo)
  134. {
  135.   /* Release the workspace. */
  136.   (*cinfo->emethods->free_small_sarray)
  137.         (pixel_row, (long) cinfo->input_components);
  138. }
  139.  
  140.  
  141. /*
  142.  * The method selection routine for input colorspace conversion.
  143.  */
  144.  
  145. GLOBAL void
  146. jselccolor (compress_info_ptr cinfo)
  147. {
  148.   /* Make sure input_components agrees with in_color_space */
  149.   switch (cinfo->in_color_space) {
  150.   case CS_GRAYSCALE:
  151.     if (cinfo->input_components != 1)
  152.       ERREXIT(cinfo->emethods, "Bogus input colorspace");
  153.     break;
  154.  
  155.   case CS_RGB:
  156.     if (cinfo->input_components != 3)
  157.       ERREXIT(cinfo->emethods, "Bogus input colorspace");
  158.     break;
  159.  
  160.   case CS_CMYK:
  161.     if (cinfo->input_components != 4)
  162.       ERREXIT(cinfo->emethods, "Bogus input colorspace");
  163.     break;
  164.  
  165.   default:
  166.     ERREXIT(cinfo->emethods, "Unsupported input colorspace");
  167.     break;
  168.   }
  169.  
  170.   /* Check num_components, set conversion method based on requested space */
  171.   switch (cinfo->jpeg_color_space) {
  172.   case CS_GRAYSCALE:
  173.     if (cinfo->num_components != 1)
  174.       ERREXIT(cinfo->emethods, "Bogus JPEG colorspace");
  175.     if (cinfo->in_color_space == CS_GRAYSCALE)
  176.       cinfo->methods->get_sample_rows = get_grayscale_rows;
  177.     else
  178.       ERREXIT(cinfo->emethods, "Unsupported color conversion request");
  179.     break;
  180.  
  181.   case CS_YCbCr:
  182.     if (cinfo->num_components != 3)
  183.       ERREXIT(cinfo->emethods, "Bogus JPEG colorspace");
  184.     if (cinfo->in_color_space == CS_RGB)
  185.       cinfo->methods->get_sample_rows = get_rgb_ycc_rows;
  186.     else
  187.       ERREXIT(cinfo->emethods, "Unsupported color conversion request");
  188.     break;
  189.  
  190.   case CS_CMYK:
  191.     if (cinfo->num_components != 4)
  192.       ERREXIT(cinfo->emethods, "Bogus JPEG colorspace");
  193.     if (cinfo->in_color_space == CS_CMYK)
  194.       cinfo->methods->get_sample_rows = get_noconvert_rows;
  195.     else
  196.       ERREXIT(cinfo->emethods, "Unsupported color conversion request");
  197.     break;
  198.  
  199.   default:
  200.     ERREXIT(cinfo->emethods, "Unsupported JPEG colorspace");
  201.     break;
  202.   }
  203.  
  204.   cinfo->methods->colorin_init = colorin_init;
  205.   cinfo->methods->colorin_term = colorin_term;
  206. }
  207.